package com.app.service.Impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.MyService;
import com.app.vo.ActivityVo;
import com.app.vo.IntegralVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyServiceImpl implements MyService {
    @Autowired
    private MyMapper myMapper;
    @Autowired
    private UserIntegralMapper userIntegralMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private GoodsExchangeMapper goodsExchangeMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ActivityVo selectMyApplication(Integer id, Integer page, Integer limit, Integer status, String name) throws ParseException {
        List<ActivityEnlistModel> activityEnlistModels = myMapper.selectMyApplicationById(id);
        List<ActivityModel> activityModels = new ArrayList<>();
        if (!activityEnlistModels.isEmpty()) {
            for (int i = 0; i < activityEnlistModels.size(); i++) {
                ActivityModel model = myMapper.selectApplication(activityEnlistModels.get(i).getActivityId());
                if (model != null) {
                    activityModels.add(model);
                }
            }
            for (int i = 0; i < activityModels.size(); i++) {
                for (int i1 = 0; i1 < activityEnlistModels.size(); i1++) {
                    if (activityModels.get(i).getId().equals(activityEnlistModels.get(i1).getActivityId())) {
                        activityModels.get(i).setAuditStatus(activityEnlistModels.get(i1).getAuditStatus());
                        if (activityModels.get(i).getAuditStatus() == 0) {
                            activityModels.get(i).setAuditStatusText("待审核");
                        }
                        if (activityModels.get(i).getAuditStatus() == 1) {
                            activityModels.get(i).setAuditStatusText("已通过");
                        }
                        if (activityModels.get(i).getAuditStatus() == 2) {
                            activityModels.get(i).setAuditStatusText("未通过");
                        }
                        activityModels.get(i).setRemark(activityEnlistModels.get(i1).getRemark());
                    }
                }
            }
            for (int i = 0; i < activityModels.size(); i++) {
                for (int i1 = 0; i1 < activityEnlistModels.size(); i1++) {
                    if (activityModels.get(i).getId().equals(activityEnlistModels.get(i1).getActivityId())) {
                        activityModels.get(i).setEnlistStatus(activityEnlistModels.get(i1).getStatus());
                    }
                }
            }
        }
        if (!activityModels.isEmpty()) {
            for (int i = 0; i < activityModels.size(); i++) {
                Integer integer = selectEnrollment(activityModels.get(i).getId());
                if (integer == null) {
                    activityModels.get(i).setEnrollment(0);
                }
                activityModels.get(i).setEnrollment(integer);

            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
        for (int i = 0; i < activityModels.size(); i++) {
            if (activityModels.get(i).getStartTime() != null && !activityModels.get(i).getStartTime().equals("")
                    && activityModels.get(i).getEndTime() != null && !activityModels.get(i).getEndTime().equals("")) {
                //开始时间
                Calendar start = Calendar.getInstance();
                Date startTime = simpleDateFormat.parse(activityModels.get(i).getStartTime());//String转Date
                start.setTime(startTime);
                Calendar end = Calendar.getInstance();
                Date endTime = simpleDateFormat.parse(activityModels.get(i).getEndTime());//String转Date
                end.setTime(endTime);
                Calendar now = Calendar.getInstance();
                String nowTime = simpleDateFormat.format(new Date());//当前时间格式化
                Date now1 = simpleDateFormat.parse(nowTime);//String转Date
                now.setTime(now1);
                if (now.before(start)) {
                    //未开始
                    activityModels.get(i).setState(1);
                    activityModels.get(i).setStateText("未开始");
                }
                if (now.after(start) && now.before(end)) {
                    //进行中
                    activityModels.get(i).setState(2);
                    activityModels.get(i).setStateText("进行中");
                }
                if (now.after(end)) {
                    //已结束
                    activityModels.get(i).setState(3);
                    activityModels.get(i).setStateText("已结束");
                }
            }
        }
        if (status == 1) {
            for (int i = 0; i < activityModels.size(); i++) {
                activityModels.get(i).setEnlistStatus(activityEnlistModels.get(i).getStatus() + 1);
            }
            List<ActivityModel> collect1 = activityModels.stream().filter(ActivityModel -> ActivityModel.getEnlistStatus() == 1).collect(Collectors.toList());
            if (name != null && !name.equals("")) {
                List<ActivityModel> collect2 = collect1.stream().filter(ActivityModel -> ActivityModel.getTitle().contains(name)).collect(Collectors.toList());
                List<ActivityModel> collect = collect2.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
                ActivityVo vo = new ActivityVo();
                vo.setList(collect);
                vo.setCount(activityEnlistModels.size());
                return vo;
            }
            List<ActivityModel> collect = collect1.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
            ActivityVo vo = new ActivityVo();
            vo.setList(collect);
            vo.setCount(activityEnlistModels.size());
            return vo;
        }
        if (status == 2) {
            for (int i = 0; i < activityModels.size(); i++) {
                activityModels.get(i).setEnlistStatus(activityEnlistModels.get(i).getStatus() + 1);
            }
            List<ActivityModel> collect1 = activityModels.stream().filter(ActivityModel -> ActivityModel.getEnlistStatus() == 2).collect(Collectors.toList());
            if (name != null && !name.equals("")) {
                List<ActivityModel> collect2 = collect1.stream().filter(ActivityModel -> ActivityModel.getTitle().contains(name)).collect(Collectors.toList());
                List<ActivityModel> collect = collect2.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
                ActivityVo vo = new ActivityVo();
                vo.setList(collect);
                vo.setCount(activityEnlistModels.size());
                return vo;
            }
            List<ActivityModel> collect = collect1.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
            ActivityVo vo = new ActivityVo();
            vo.setList(collect);
            vo.setCount(activityEnlistModels.size());
            return vo;
        }
        if (status == 3) {
            for (int i = 0; i < activityModels.size(); i++) {
                activityModels.get(i).setEnlistStatus(activityEnlistModels.get(i).getStatus() + 1);
            }
            List<ActivityModel> collect1 = activityModels.stream().filter(ActivityModel -> ActivityModel.getEnlistStatus() == 3).collect(Collectors.toList());
            if (name != null && !name.equals("")) {
                List<ActivityModel> collect2 = collect1.stream().filter(ActivityModel -> ActivityModel.getTitle().contains(name)).collect(Collectors.toList());
                List<ActivityModel> collect = collect2.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
                ActivityVo vo = new ActivityVo();
                vo.setList(collect);
                vo.setCount(activityEnlistModels.size());
                return vo;
            }
            List<ActivityModel> collect = collect1.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
            ActivityVo vo = new ActivityVo();
            vo.setList(collect);
            vo.setCount(activityEnlistModels.size());
            return vo;
        }
        if (name != null && !name.equals("")) {
            List<ActivityModel> collect2 = activityModels.stream().filter(ActivityModel -> ActivityModel.getTitle().contains(name)).collect(Collectors.toList());
            List<ActivityModel> collect = collect2.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
            ActivityVo vo = new ActivityVo();
            vo.setList(collect);
            vo.setCount(activityEnlistModels.size());
            return vo;
        }
        for (int i = 0; i < activityModels.size(); i++) {
            activityModels.get(i).setEnlistStatus(activityEnlistModels.get(i).getStatus() + 1);
        }
        List<ActivityModel> collect = activityModels.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
        ActivityVo vo = new ActivityVo();
        vo.setList(collect);
        vo.setCount(activityEnlistModels.size());
        return vo;
    }

    @Override
    public IntegralVo selectMyIntegral(Integer id, Integer page, Integer limit) {
        QueryWrapper<UserIntegralModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        queryWrapper.orderByDesc("create_time");
        List<UserIntegralModel> userIntegralModels = userIntegralMapper.selectList(queryWrapper);
        for (int i = 0; i < userIntegralModels.size(); i++) {
            if (userIntegralModels.get(i).getType() == 1) {
                ActivityEnlistModel model = activityEnlistMapper.selectById(userIntegralModels.get(i).getAssociatedId());
                ActivityModel activityModel = activityMapper.selectById(model.getActivityId());
                userIntegralModels.get(i).setTitle("【活动报名】" + activityModel.getTitle());
                userIntegralModels.get(i).setIntegralText("+" + userIntegralModels.get(i).getIntegral());
                userIntegralModels.get(i).setStatusText("");
            }
            if (userIntegralModels.get(i).getType() == 3) {
                GoodsExchangeModel model = goodsExchangeMapper.selectById(userIntegralModels.get(i).getAssociatedId());
                if (model.getStatus() != null && model.getStatus() == 0) {
                    userIntegralModels.get(i).setStatus(model.getStatus());
                    userIntegralModels.get(i).setStatusText("待审核");
                }
                if (model.getStatus() != null && model.getStatus() == 1) {
                    userIntegralModels.get(i).setStatus(model.getStatus());
                    userIntegralModels.get(i).setStatusText("已核销");
                }
                if (model.getStatus() != null && model.getStatus() == 2) {
                    userIntegralModels.get(i).setStatus(model.getStatus());
                    userIntegralModels.get(i).setStatusText("未通过");
                }
                GoodsModel goodsModel = goodsMapper.selectById(model.getGoodsId());
                userIntegralModels.get(i).setTitle("【兑换商品】" + goodsModel.getName() + " × " + model.getNum());
                userIntegralModels.get(i).setIntegralText("-" + userIntegralModels.get(i).getIntegral());
                userIntegralModels.get(i).setNum(model.getNum());
            }
        }
        List<UserIntegralModel> collect = userIntegralModels.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
        UserModel userModel = userMapper.selectById(id);
        IntegralVo vo = new IntegralVo();
        vo.setIntegral(userModel.getIntegral());
        vo.setList(collect);
        vo.setCount(userIntegralModels.size());
        return vo;
    }

    private Integer selectEnrollment(Integer activityId) {
        return activityEnlistMapper.selectEnrollment(activityId);
    }
}
